package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.Admin;
import com.example.mybatisplus.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.AdminService;
import com.example.mybatisplus.model.domain.Admin;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2021-07-25
 * @version v1.0
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    private final Logger logger = LoggerFactory.getLogger( AdminController.class );

    @Autowired
    private AdminService adminService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Admin  admin =  adminService.getById(id);
        return JsonResponse.success(admin);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        adminService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateAdmin(@PathVariable("id") Long  id,Admin  admin) throws Exception {
        admin.setId(id);
        adminService.updateById(admin);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Admin
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Admin  admin) throws Exception {
        adminService.save(admin);
        return JsonResponse.success(null);
    }
    @RequestMapping("/list")
    @ResponseBody
    public JsonResponse list(Model model,
                             @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                             @RequestParam(required = false,defaultValue = "10")Integer pageSize){
        Page<Admin> page = adminService.page(new Page<>(pageNo, pageSize));

        return JsonResponse.success(page);
    }

    @RequestMapping("/deleteByIds")
    @ResponseBody
    public JsonResponse deleteByIds(@RequestParam("ids[]") List<Integer> ids){
        adminService.removeByIds(ids);
        return JsonResponse.success( null);
    }

    @GetMapping("/userLogin")
    public  String userLogin(Admin admin){
        //获取用户名密码

        //去调用AdminService里的方法，必须要有这个实列对象

        Admin admin1=adminService.userLogin(admin);

        if (admin1==null){
            return "/login";
        }else{
            return "/index";
        }
    }
    @ResponseBody
    @PostMapping("/loginByAjax")
    public  JsonResponse loginByAjax(Admin admin){
        Admin admin1=adminService.userLogin(admin);
        SessionUtils.saveCurrentUserInfo(admin1);
        return  JsonResponse.success(admin1);
    }

    @RequestMapping("/reg")
    public String index(){
        return "/register";
    }

    @RequestMapping("/forgot")
    public String forgot(){
        return "/forget";
    }



    @ResponseBody
    @PostMapping("/register")
    public  JsonResponse register(Admin admin){
         Admin admin1=adminService.userRegister(admin);
        if (admin1==null){
            adminService.save(admin);
        }

        return  JsonResponse.success(admin1);
    }
    @ResponseBody
    @PostMapping("/findPwd")
    public  JsonResponse findPwd(Admin admin){
            Admin admin1=adminService.userFind(admin);
            if (admin1!=null){
                admin1.setPassword(admin.getPassword());
                admin1.updateById();
        }
            return  JsonResponse.success(admin1);
    }

    @ResponseBody
    @PostMapping("/change")
    public JsonResponse change(Admin admin){
        Admin admin1=adminService.userChange(admin);
        if(admin1 != null) {
            admin1.setPhone(admin.getPhone());
            admin1.setAvatar(admin.getAvatar());
            admin1.updateById();
            SessionUtils.saveCurrentUserInfo(admin1);
        }
        return  JsonResponse.success(admin1);
    }
    @ResponseBody
    @PostMapping("/pwdChange")
    public JsonResponse pwdChange(String oldPwd,String newPwd){
        Admin admin1=adminService.pwdChange(oldPwd);
        if(admin1 != null) {
            admin1.setPassword(newPwd);
            admin1.updateById();
            SessionUtils.saveCurrentUserInfo(admin1);
        }
        return  JsonResponse.success(admin1);
    }
}

